import {ElMessage} from "element-plus";

export default class Func {
    static clearObject = (obj) => {
        if (!obj || typeof obj !== 'object') return {};

        return Object.fromEntries(
            Object.entries(obj).filter(([_, v]) => (
                v !== '' && v !== null && v !== undefined
            ))
        );
    }

    static isEmpty = (obj) => {
        return obj === null || obj === '' || obj === undefined;
    }

    static isAnyEmpty(value) {
        // 处理 null 和 undefined
        if (value === null || value === undefined) {
            return true;
        }

        // 处理字符串
        if (typeof value === 'string') {
            return value.trim() === '';
        }

        // 处理数组
        if (Array.isArray(value)) {
            return value.length === 0;
        }

        // 处理对象
        if (typeof value === 'object') {
            // 检查对象自身属性（不包括原型链）
            for (let key in value) {
                if (value[key] === null || value[key] === undefined || value[key] === '') {
                    return true;
                }
            }
            return false;
        }
        return false;
    }

    static queryFinish = () => {
        ElMessage.success('查询完成')
    }

    static refreshFinish = () => {
        ElMessage.success('刷新成功')
    }

    static dateFormat(dateStr) {
        const inputDate = new Date(dateStr);
        const now = new Date();

        if (isNaN(inputDate.getTime())) {
            return '无效日期';
        }

        const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());
        const inputDayStart = new Date(inputDate.getFullYear(), inputDate.getMonth(), inputDate.getDate());
        const dayDiff = Math.floor((todayStart - inputDayStart) / (1000 * 60 * 60 * 24));

        const timeString = `${inputDate.getHours().toString().padStart(2, '0')}:${inputDate.getMinutes().toString().padStart(2, '0')}:${inputDate.getSeconds().toString().padStart(2, '0')}`;

        switch (dayDiff) {
            case 0:
                return `今天 ${timeString}`;
            case 1:
                return `昨天 ${timeString}`;
            case 2:
                return `前天 ${timeString}`;
            default:
                const month = (inputDate.getMonth() + 1).toString().padStart(2, '0');
                const day = inputDate.getDate().toString().padStart(2, '0');

                // 如果是跨年的，显示年份
                if (inputDate.getFullYear() !== now.getFullYear()) {
                    const year = inputDate.getFullYear();
                    return `${year}-${month}-${day} ${timeString}`;
                }

                return `${month}-${day} ${timeString}`;
        }
    }



}